﻿using System;

using NUnit.Framework;

using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;

namespace mvcForum.Tests.WebUI {

	[TestFixture]
	public class C003_ForumTests {
		private IWebDriver driver;
		private const String forumName = "My new forum";
		private const Int32 sortOrder = 99;
		private const String description = "A some what lame description for a forum!";

		#region Initialization and cleanup
		[TestFixtureSetUp]
		public void TestInitialize() {
			driver = TestHelper.GetDriver();
		}

		[TestFixtureTearDown]
		public void TestCleanup() {
			if (driver != null) {
				driver.Close();
			}
		}
		#endregion

		// TODO: EditForum test!

		[Test]
		public void CreateForum() {
			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.VisitURL("forumadmin", "stats");

			IWebElement row = driver.FindBoardRows()[0];
			String boardName = row.FindElements(By.TagName("td"))[0].Text;
			row.FindElement(By.LinkText("Edit")).Click();

			WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 0, 10));
			wait.Until(drv => drv.FindElement(By.Id("boardsec")));

			row = driver.FindCategoryRows()[0];
			String categoryName = row.FindElements(By.TagName("td"))[0].Text;
			row.FindElement(By.LinkText("Edit")).Click();

			wait = new WebDriverWait(driver, new TimeSpan(0, 0, 10));
			wait.Until(drv => drv.FindElement(By.Id("categorysec")));

			try {
				// Create pop-up should be invisible!
				driver.FindElement(By.LinkText("Create"));
				Assert.AreEqual(false, true, "The create button was found");
			}
			catch {
				Assert.AreEqual(true, true, "No create button");
			}

			IWebElement addLink = driver.FindElement(By.LinkText("Add Forum"));
			addLink.Click();

			try {
				// Create pop-up should be visible!
				driver.FindElement(By.LinkText("Create"));
				Assert.AreEqual(true, true, "The create button was found");
			}
			catch {
				Assert.AreEqual(false, true, "No create button found");
			}

			IWebElement closeLink = driver.FindElement(By.LinkText("Close"));
			closeLink.Click();

			try {
				// Create pop-up should be invisible!
				driver.FindElement(By.LinkText("Create"));
				Assert.AreEqual(false, true, "The create button was found");
			}
			catch {
				Assert.AreEqual(true, true, "No create button");
			}

			addLink.Click();

			IWebElement modal = driver.FindElement(By.Id("newforummodal"));

			IWebElement sortOrderField = modal.FindElement(By.Name("SortOrder"));
			driver.InsertText(sortOrderField, "");

			IWebElement createLink = driver.FindElement(By.LinkText("Create"));
			createLink.Click();

			Assert.AreEqual(true, driver.PageSource.Contains("The forum name is required!"), "The error: \"forum name required\" is shown");
			Assert.AreEqual(true, driver.PageSource.Contains("The sort order is required!"), "The error: \"sort order required\" is shown");
			Assert.AreEqual(false, driver.PageSource.Contains("A new forum was created successfully!"), "The forum was create without required values.");

			sortOrderField = modal.FindElement(By.Name("SortOrder"));
			driver.InsertText(sortOrderField, "d");
			createLink.Click();

			Assert.AreEqual(true, driver.PageSource.Contains("The sort order must be an integer!"), "The error: \"sort order integer\" is shown");

			closeLink = driver.FindElement(By.LinkText("Close"));
			closeLink.Click();

			addLink.Click();

			IWebElement nameField = modal.FindElement(By.Name("Name"));
			driver.InsertText(nameField, forumName);
			sortOrderField = modal.FindElement(By.Name("SortOrder"));
			driver.InsertText(sortOrderField, sortOrder.ToString());
			IWebElement descriptionField = modal.FindElement(By.Name("Description"));
			driver.InsertText(descriptionField, description);

			createLink.Click();

			driver.Wait(2);

			Assert.AreEqual(true, driver.PageSource.Contains("A new forum was created successfully!"), "Created successfully text displayed");

			driver.EditForum(boardName, categoryName, forumName);

			Assert.AreEqual(forumName, driver.GetTextBoxValueByName("Name"), "Name");
			Assert.AreEqual(sortOrder.ToString(), driver.GetTextBoxValueByName("SortOrder"), "Sort order");
			Assert.AreEqual(description, driver.GetTextAreaValueById("Description"), "Description");

			driver.LogOff();
		}

		[Test]
		public void DeleteForum() {
			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.VisitURL("forumadmin", "stats");

			IWebElement row = driver.FindBoardRows()[0];
			String boardName = row.FindElement(By.TagName("td")).Text;
			row.FindElement(By.LinkText("Edit")).Click();

			WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 0, 10));
			wait.Until(drv => drv.FindElement(By.Id("boardsec")));

			row = driver.FindCategoryRows()[0];
			String categoryName = row.FindElement(By.TagName("td")).Text;
			row.FindElement(By.LinkText("Edit")).Click();

			wait = new WebDriverWait(driver, new TimeSpan(0, 0, 10));
			wait.Until(drv => drv.FindElement(By.Id("categorysec")));

			try {
				// Create pop-up should be invisible!
				driver.FindElement(By.LinkText("OK"));
				Assert.AreEqual(false, true);
			}
			catch {
				Assert.AreEqual(true, true);
			}

			row = driver.FindForumRow(forumName);
			IWebElement deleteLink = row.FindElement(By.LinkText("Delete"));
			deleteLink.Click();

			try {
				// Create pop-up should be invisible!
				driver.FindElement(By.LinkText("OK"));
				Assert.AreEqual(true, true);
			}
			catch {
				Assert.AreEqual(false, true);
			}

			IWebElement link = driver.FindElement(By.LinkText("OK"));
			link.Click();

			driver.EditCategory(boardName, categoryName);

			Assert.AreEqual(null, driver.FindForumRow(forumName), "Forum deleted");

			driver.LogOff();
		}
	}
}